package com.ivy.irublesson.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.ivy.irublesson.R;
import com.ivy.irublesson.adapter.TeacherAdapter;
import com.ivy.irublesson.entity.School;
import com.ivy.irublesson.utils.HttpUtil;
import com.ivy.irublesson.utils.MyOpenHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class StartActivity extends AppCompatActivity {
    private List<School> schools = new ArrayList<>();
    private Handler handler;
    private MyOpenHelper openHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

//        //插入数据库
//        openHelper = new MyOpenHelper(this, "irublesson.db", null, 1);
//        SQLiteDatabase database = openHelper.getReadableDatabase();
//        Thread schoolThread = new Thread(new SchoolThread());
//        schoolThread.start();
//        for(School s:schools){
//            int id = s.getId();
//            String sname = s.getSname();
//            String listpath = s.getListpath();
//            String listcookie = s.getListcookie();
//            String referer = s.getReferer();
//            String cookiepath = s.getCookiepath();
//            String coursepath = s.getCoursepath();
//            String sql1 = "insert into school(id,sname,listpath , listcookie ,referer,cookiepath ,coursepath) " +
//                    "values( "+id+" ,'"+sname+"' ,'"+listpath+"' ,'"+listcookie+"' ,'"+referer+"','"+cookiepath+"' , '"+coursepath+"' )";
//            database.execSQL(sql1);
//            Toast.makeText(StartActivity.this, "插入 " +schools.get(1).getSname(), Toast.LENGTH_SHORT).show();
//        }
//        database.close();
//
//        handler = new Handler(){
//            @Override
//            public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 1:
//                    Handler x = new Handler();
//                    x.postDelayed(new splashhandler(), 3000);
//                    break;
//            }
//            }
//        };

//        Thread schoolThread = new Thread(new SchoolThread());
//        schoolThread.start();
//        handler = new Handler(){
//                        @Override
//            public void handleMessage(Message msg) {
//            switch (msg.what){
//                case 1:
//                    Handler x = new Handler();
//                    x.postDelayed(new splashhandler(), 3000);
//                    break;
//            }
//            }
//        };


        Handler x = new Handler();
        x.postDelayed(new splashhandler(), 3000);
    }

    private String schoolPath = "http://119.23.232.105:8080/IRubLesson/SchoolServlet";
    class SchoolThread implements Runnable {
        @Override
        public void run() {
            try {
                JSONArray arr = HttpUtil.getJSONArrayByURL(schoolPath);
                for(int i = 0;i < arr.length();i++) {
                    JSONObject obj = arr.getJSONObject(i);
                    School school = new School();
                    school.setId(obj.getInt("id"));
                    school.setSname(obj.getString("sname"));
                    school.setListpath(obj.getString("listpath"));
                    school.setListcookie(obj.getString("listcookie"));
                    school.setReferer(obj.getString("referer"));
                    school.setCookiepath(obj.getString("cookiepath"));
                    school.setCoursepath(obj.getString("coursepath"));
                    //  map.put("owner_tel", obj.getString("owner_tel"));
                    schools.add(school);

                }
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    class splashhandler implements Runnable {
        public void run() {
            startActivity(new Intent(getApplication(), InitActivity.class));
            StartActivity.this.finish();
        }
    }
}
